var express = require('express');
var router = express.Router();
/// 引入数据库模块
var _db = require('../mysql.js');
/// 引入全局变量模块
var other = require('../other.js');

/* GET home page. */
router.get('/', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    res.render('management', { title: 'Express', _adminname: other._adminname });
  }
});

/// 高校信息管理
router.get('/school', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM school', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_School', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 高校信息管理 - 查询
router.get('/school/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var schoolname = req.query.select;
    _db.query(`SELECT * FROM school WHERE SchoolName LIKE '%${schoolname}%'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_School', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 高校信息管理 - 添加界面
router.get('/school/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM school', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_School', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 高校信息管理 - 添加按钮
router.post('/school/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到用户输入的内容
    var val = req.body;
    var _value = val.schoolname;
    _db.query("INSERT INTO school(SchoolName) VALUES (?)", [_value], function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });
  }
});

/// 高校信息管理 - 删除按钮
router.get('/school/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var _value = req.query.delete;
    _db.query(`DELETE FROM school WHERE SchoolID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../school\');}, 2500);</script>');
      }
    });
  }
});

/// 园区信息管理
router.get('/area', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_area', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Area', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });

  }
});

/// 园区信息管理 - 查询
router.get('/area/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var Dorm_AreaName = req.query.select;
    _db.query(`SELECT * FROM dorm_area Where Dorm_AreaName LIKE '%${Dorm_AreaName}%'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Area', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 园区信息管理 - 添加界面
router.get('/area/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_area', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Area', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 园区信息管理 - 添加按钮
router.post('/area/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到园区输入的内容
    var val = req.body;
    var _value = val.Dorm_AreaName;
    var _value2 = val.SchoolID;
    _db.query("INSERT INTO dorm_area(Dorm_AreaName,SchoolID) VALUES (?,?)", [_value, _value2], function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });
  }
});

/// 园区信息管理 - 删除按钮
router.get('/area/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var _value = req.query.delete;
    _db.query(`DELETE FROM dorm_area WHERE Dorm_AreaID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../area\');}, 2500);</script>');
      }
    });
  }
});

/// 楼栋信息管理
router.get('/build', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_build', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Build', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });

  }
});

/// 楼栋信息管理 - 删除按钮
router.get('/build/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var _value = req.query.delete;
    _db.query(`DELETE FROM dorm_build WHERE Dorm_BuildID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../build\');}, 2500);</script>');
      }
    });
  }
});




/// 楼栋信息管理 - 查询
router.get('/build/main', function (req, res, next) {
  var Dorm_BuildName = req.query.select;
  _db.query(`SELECT * FROM dorm_build Where Dorm_BuildName LIKE '%${Dorm_BuildName}%'`, function (err, results) {
    if (err) {
      throw err;
    } else {
      res.render('adm_Dorm_Build', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
    }
  });
});

/// 楼栋信息管理 - 添加界面
router.get('/build/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_build', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Build', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 楼栋信息管理 - 添加按钮
router.post('/build/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到楼栋输入的内容
    var val = req.body;
    var _value = val.Dorm_BuildName;
    var _value2 = val.Dorm_AreaID;
    _db.query("INSERT INTO dorm_build(Dorm_BuildName,Dorm_AreaID) VALUES (?,?)", [_value, _value2], function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });

  }
});

/// 宿舍信息管理
router.get('/room', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_room', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Room', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });

  }
});

/// 宿舍信息管理 - 删除按钮
router.get('/room/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    var _value = req.query.delete;
    _db.query(`DELETE FROM dorm_room WHERE Dorm_RoomID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../room\');}, 2500);</script>');
      }
    });
  }
});



/// 宿舍信息管理 - 查询
router.get('/room/main', function (req, res, next) {
  /// 获取查询ID
  var Dorm_RoomName = req.query.select;
  _db.query(`SELECT * FROM dorm_room Where Dorm_RoomName LIKE '%${Dorm_RoomName}%'`, function (err, results) {
    if (err) {
      throw err;
    } else {
      res.render('adm_Dorm_Room', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
    }
  });
}
);

/// 宿舍信息管理 - 添加界面
router.get('/room/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM dorm_room', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Dorm_Room', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 宿舍信息管理 - 添加按钮
router.post('/room/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到宿舍输入的内容
    var val = req.body;
    var _value = val.Dorm_RoomName;
    var _value2 = val.Dorm_BuildID;
    _db.query("INSERT INTO dorm_room(Dorm_RoomName,Dorm_BuildID) VALUES (?,?)", [_value, _value2], function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });
  }
});


/// 学生信息管理
router.get('/student', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM students', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_StudentsInfo', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 学生信息管理 - 查询
router.get('/student/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var StuName = req.query.select;
    _db.query(`SELECT * FROM students Where StuName LIKE '%${StuName}%'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_StudentsInfo', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 学生信息管理 - 添加界面
router.get('/student/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM students', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_StudentsInfo', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 学生信息管理 - 添加按钮
router.post('/student/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到学生输入的内容
    var val = req.body;
    var _value = val.StuName;
    var _value2 = val.gender;
    var _value3 = val.StuNum;
    var _value4 = val.PassWord;
    var _value5 = val.SchoolID;
    var _value6 = val.ClassName;
    var _value7 = val.Dorm_AreaID;
    var _value8 = val.Dorm_BuildID;
    var _value9 = val.Dorm_RoomID;
    _db.query("INSERT INTO students(StuName,gender,StuNum,PassWord,SchoolID,ClassName,Dorm_AreaID,Dorm_BuildID,Dorm_RoomID) VALUES (?,?,?,?,?,?,?,?,?)", [_value, _value2, _value3, _value4, _value5, _value6, _value7, _value8, _value9], function (err, results) {
      console.log([_value, _value2, _value3, _value4, _value5, _value6, _value7, _value8, _value9]);
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });
  }
});

/// 学生信息管理 - 删除按钮
router.get('/student/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var _value = req.query.delete;
    _db.query(`DELETE FROM students WHERE StuID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../student\');}, 2500);</script>');
      }
    });
  }
});

/// 订单信息管理
router.get('/order', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM orders', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Orders', { title: 'Express', _adminname: other._adminname, DB: results });
      }
    });
  }
});

/// 订单信息管理 - 操作
router.get('/order/state', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取链接中的内容
    var val = req.query;
    var type = val.type; /// 获取操作类型
    var value = val.value; /// 获取操作值
    var sql = ""; /// 设置语句
    console.log([type,value]);
    if(type=="Rejection"){ /// 拒单：-1
      sql = `UPDATE orders SET State = -1 WHERE OrderID = ${value}`;
    } else if(type=="take_orders") { /// 接单：1
      sql = `UPDATE orders SET State = 2 WHERE OrderID = ${value}`;
    } else if(type=="distribution") { /// 配送：2
      sql = `UPDATE orders SET State = 3 WHERE OrderID = ${value}`;
    } else if(type=="completed") { /// 完成：3
      sql = `UPDATE orders SET State = 4 WHERE OrderID = ${value}`;
    } else if(type=="delete") { /// 删除订单
      sql = `DELETE FROM orders WHERE OrderID = ${value}`;
    }
    _db.query(sql, function (err, results) {
      if (err) {
        throw err;
      } else {
        if(results.affectedRows<1){
          res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
          res.end('<span style="font-size: 50px;">操作失败，请检查参数！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../order\');}, 2500);</script>');
        } else {
          res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
          res.end('<span style="font-size: 50px;">操作成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../order\');}, 2500);</script>');
        }
        // res.render('adm_Orders', { title: 'Express', _adminname: other._adminname, DB: results });

      }
    });
  }
});

/// 商品信息管理
router.get('/commodity', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM commodity', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Commodity', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 商品信息管理 - 查询
router.get('/commodity/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var CommodityName = req.query.select;
    _db.query(`SELECT * FROM commodity Where CommodityName LIKE '%${CommodityName}%'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Commodity', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '0' });
      }
    });
  }
});

/// 商品信息管理 - 添加界面
router.get('/commodity/insert', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM commodity', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Commodity', { title: 'Express', _adminname: other._adminname, DB: results, ck_two_sql: '1' });
      }
    });
  }
});

/// 商品信息管理 - 添加按钮
router.post('/commodity/insert/main', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取到商品输入的内容
    var val = req.body;
    var _value = val.CommodityName;
    var _value2 = val.CommodityCapacity;
    var _value3 = val.CommodityImg;
    var _value4 = val.CommodityPrice;
    _db.query("INSERT INTO commodity(CommodityName,CommodityCapacity,CommodityImg,CommodityPrice) VALUES (?,?,?,?)", [_value, _value2, _value3, _value4], function (err, results) {
      console.log([_value, _value2, _value3, _value4]);
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">添加成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../\');}, 2500);</script>');
      }
    });
  }
});

/// 商品信息管理 - 删除按钮
router.get('/commodity/delete', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取查询ID
    var _value = req.query.delete;
    _db.query(`DELETE FROM commodity WHERE CommodityID = '${_value}'`, function (err, results) {
      if (err) {
        throw err;
      } else {
        res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
        res.end('<span style="font-size: 50px;">删除成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../commodity\');}, 2500);</script>');
      }
    });
  }
});

/// 个人信息
router.get('/myinfo', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM admin', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_MyInfo', { title: 'Express', _adminname: other._adminname, DB: results });
      }
    });
  }
});

/// 修改密码
router.get('/changepwd', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._adminname == "-null") {
    res.render('login');
    return;
  } else {
    _db.query('SELECT * FROM admin', function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('adm_Change_PassWord', { title: 'Express', _adminname: other._adminname, DB: results });
      }
    });
  }
});

/// 退出登录
router.get('/loginOut', function (req, res, next) {
  other._adminname = '-null';
  /// 暴露信息 方便其他路由调用 以防造成退出了还有登录信息
  module.exports = other._adminname;
  /// 跳转主页
  res.redirect('/');
});

module.exports = router;
